#include "event_plugin.h"
#include "ui_event_plugin.h"

#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>

#include <utils/topic.h>

#include <muduo/net/EventLoop.h>
#include <muduo/net/EventLoopThread.h>

#include <set>

namespace ui {
EventPlugin::EventPlugin(QWidget* parent)
    : rviz_common::Panel(parent)
    , ui(new Ui::Form)
{
    ui->setupUi(this);

    label = new QLabel(this);
    label->setAlignment(Qt::AlignCenter);
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    auto layout = new QGridLayout;
    layout->addWidget(label);

    setLayout(layout);

    eventLoopThread_ = std::make_shared<muduo::net::EventLoopThread>();
    eventLoopThread_->startLoop()->runInLoop([=] { rosInit(); });

    // std::set<std::string> strs { "全向碰撞预警" };

    // size_t index = 0;
    // auto layout = new QGridLayout;

    // for (auto& i : strs) {
    //     auto label = new QLabel(this);
    //     label->setText(i.c_str());
    //     label->setAlignment(Qt::AlignCenter);
    //     label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    //     layout->addWidget(label, 1, 1 + 3 * index);
    //     layout->addItem(new QSpacerItem(40, 20, QSizePolicy::Maximum, QSizePolicy::Fixed), 1, 2 + 3 * index);
    //     layout->addItem(new QSpacerItem(40, 20, QSizePolicy::Maximum, QSizePolicy::Fixed), 1, 3 * index);

    //     layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Maximum), 0, 1 + 3 * index);
    //     layout->addItem(new QSpacerItem(20, 20, QSizePolicy::Fixed, QSizePolicy::Maximum), 2, 1 + 3 * index);

    //     index++;
    // }

    // setLayout(layout);
}

void EventPlugin::rosInit(void)
{
    node_ = std::make_shared<rclcpp::Node>("EventPlugin");

    timer_ = node_->create_wall_timer(std::chrono::milliseconds(200), std::bind(&EventPlugin::onTimer, this));

    eventSubscriber_ = node_->create_subscription<interface::msg::Event>(eventTopic, 10, std::bind(&EventPlugin::onEvent, this, std::placeholders::_1));

    rclcpp::spin(node_);
}

void EventPlugin::onTimer(void)
{
    if (eventCounter_++ >= 5) {
        for (auto& i : eventDirectFlags_) {
            i = false;
        }
    }

    drawEventTitle();
}

void EventPlugin::onEvent(std::shared_ptr<interface::msg::Event> msg)
{
    eventCounter_ = 0;

    std::string title;

    for (auto begin = msg->vehicles.begin(); begin != msg->vehicles.end(); begin++) {
        auto azimuth = begin->azimuth;

        if ((azimuth >= 0 && azimuth <= 15) || (azimuth > 345 && azimuth <= 360)) {
            eventDirectFlags_[0] = true;
        } else if (azimuth > 15 && azimuth <= 75) {
            eventDirectFlags_[1] = true;
        } else if (azimuth > 75 && azimuth <= 105) {
            eventDirectFlags_[2] = true;
        } else if (azimuth > 105 && azimuth <= 165) {
            eventDirectFlags_[3] = true;
        } else if (azimuth > 165 && azimuth <= 195) {
            eventDirectFlags_[4] = true;
        } else if (azimuth > 195 && azimuth <= 255) {
            eventDirectFlags_[5] = true;
        } else if (azimuth > 255 && azimuth <= 285) {
            eventDirectFlags_[6] = true;
        } else if (azimuth > 285 && azimuth <= 345) {
            eventDirectFlags_[7] = true;
        }
    }
}

void EventPlugin::drawEventTitle(void)
{
    static std::vector<std::string> directs {
        "前方", "右前方", "右方", "右后方", "后方", "左后方", "左方", "左前方"
    };

    std::string str;
    bool isAlarm { false };

    for (size_t i = 0; i < (sizeof(eventDirectFlags_) / sizeof(eventDirectFlags_[0])); i++) {
        if (eventDirectFlags_[i] == false) {
            continue;
        }

        str.append(directs[i]);
        str.append("、");
        isAlarm = true;
    }

    if (!str.empty()) {
        str = std::string(str.data(), str.size() - 1);
    }

    str.append("有碰撞风险");

    if (isAlarm) {
        label->setText(str.c_str());
        label->setStyleSheet("background-color: rgb(255, 255, 255);font-size:40px;color:red");
    } else {
        label->setText("无碰撞风险");
        label->setStyleSheet("background-color: rgb(255, 255, 255);font-size:40px;color:green");
    }
}

EventPlugin::~EventPlugin()
{
    rclcpp::shutdown();
    delete ui;
}

}

#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(ui::EventPlugin, rviz_common::Panel)
